﻿using System;
using System.Net;

namespace UpdateBot2.FileHippo
{
    /// <summary>
    /// Extends WebClient and adds minor support for FileHippo.com Filter cookies and cookies in general.
    /// </summary>
    class FileHippoWebClient : WebClient
    {
        private readonly CookieContainer m_container = new CookieContainer();

        public FileHippoWebClient(Boolean noBeta, Boolean noDemo)
            : base()
        {
            this.ApplyFilterCookie(noBeta, noDemo);
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            HttpWebRequest webRequest = request as HttpWebRequest;
            if (webRequest != null)
            {
                webRequest.CookieContainer = m_container;
            }
            return request;
        }

        /// <summary>
        /// Sets beta and freeware flags; 1 = true, 0 = false
        /// </summary>
        /// <param name="noBeta"></param>
        /// <param name="noDemo"></param>
        public void ApplyFilterCookie(Boolean noBeta, Boolean noDemo)
        {
            String noBetaStr = (noBeta) ? "1" : "0";
            String noDemoStr = (noDemo) ? "1" : "0";

            Cookie c = new Cookie();
            c.Name = "Filter";
            c.Domain = "filehippo.com";
            c.Value = String.Format("NOBETA={0}&NODEMO={1}", noBetaStr, noDemoStr);

            this.m_container.Add(c);
        }

    }
}
